
#ifndef MSE_V_DBUS_H
#define MSE_V_DBUS_H

#include "stdint.h"
#include "MSE_MATH.h"
#include "MY_define.h"
#include "string.h"
#include "gpio.h"

#ifndef PassState

#define MSE_DF_KEY_CLICK     1 // 点按
#define MSE_DF_KEY_UP        0 // 松开
#define MSE_DF_KEY_PRESS     2 // 长按

#endif

#define MSE_Mouse_VX MSE_V_DBUS.Mouse.X_Flt // 鼠标横向速度
#define MSE_Mouse_VY MSE_V_DBUS.Mouse.Y_Flt // 鼠标纵向速度
#define MSE_Mouse_L  MSE_V_DBUS.Mouse.R_State
#define MSE_Mouse_R  MSE_V_DBUS.Mouse.L_State
//按下为1松开为0  长按
#define MSE_KeyBoard_A     MSE_V_DBUS.KeyBoard.A     // a键调用
#define MSE_KeyBoard_S     MSE_V_DBUS.KeyBoard.S     // s键调用
#define MSE_KeyBoard_D     MSE_V_DBUS.KeyBoard.D     // d键调用
#define MSE_KeyBoard_W     MSE_V_DBUS.KeyBoard.W     // w键调用
#define MSE_KeyBoard_Q     MSE_V_DBUS.KeyBoard.Q     // q键调用
#define MSE_KeyBoard_E     MSE_V_DBUS.KeyBoard.E     // e键调用
#define MSE_KeyBoard_R     MSE_V_DBUS.KeyBoard.R     // r键调用
#define MSE_KeyBoard_F     MSE_V_DBUS.KeyBoard.F     // r键调用
#define MSE_KeyBoard_T     MSE_V_DBUS.KeyBoard.T     // t键调用
#define MSE_KeyBoard_G     MSE_V_DBUS.KeyBoard.G     // g键调用
#define MSE_KeyBoard_Z     MSE_V_DBUS.KeyBoard.Z     // z键调用
#define MSE_KeyBoard_X     MSE_V_DBUS.KeyBoard.X     // x键调用
#define MSE_KeyBoard_C     MSE_V_DBUS.KeyBoard.C     // c键调用
#define MSE_KeyBoard_V     MSE_V_DBUS.KeyBoard.V     // v键调用
#define MSE_KeyBoard_B     MSE_V_DBUS.KeyBoard.B     // b键调用
#define MSE_KeyBoard_Ctrl  MSE_V_DBUS.KeyBoard.Ctrl  // Ctrl键调用
#define MSE_KeyBoard_Shift MSE_V_DBUS.KeyBoard.Shift // shift键调用
// 按下为1，再次按下为0 短按
#define MSE_KeyBoard_Q_Lock     MSE_V_DBUS.KeyBoard.Q_PreeNumber
#define MSE_KeyBoard_E_Lock     MSE_V_DBUS.KeyBoard.E_PreeNumber
#define MSE_KeyBoard_R_Lock     MSE_V_DBUS.KeyBoard.R_PreeNumber
#define MSE_KeyBoard_F_Lock     MSE_V_DBUS.KeyBoard.F_PreeNumber
#define MSE_KeyBoard_G_Lock     MSE_V_DBUS.KeyBoard.G_PreeNumber
#define MSE_KeyBoard_Z_Lock     MSE_V_DBUS.KeyBoard.Z_PreeNumber
#define MSE_KeyBoard_X_Lock     MSE_V_DBUS.KeyBoard.X_PreeNumber
#define MSE_KeyBoard_C_Lock     MSE_V_DBUS.KeyBoard.C_PreeNumber
#define MSE_KeyBoard_V_Lock     MSE_V_DBUS.KeyBoard.V_PreeNumber
#define MSE_KeyBoard_B_Lock     MSE_V_DBUS.KeyBoard.B_PreeNumber
#define MSE_KeyBoard_Shift_Lock MSE_V_DBUS.KeyBoard.Shift_PreeNumber
#define MSE_KeyBoard_Ctrl_Lock  MSE_V_DBUS.KeyBoard.Ctrl_PreeNumber

struct DBUS_Typedef
{
    int8_t ONLINE_JUDGE_TIME; // 在线检测时间
    //遥控
    struct
    {
        int16_t CH0_int16;
        int16_t CH1_int16;
        int16_t CH2_int16;
        int16_t CH3_int16;
        int16_t Dial_int16;
        uint8_t S1_u8;
        uint8_t S2_u8;
        int8_t Error_int8;
    } Remote;
	
	  struct
    {
        float X_Flt;
        float X_Filter; // 处理后使用的鼠标值
        float X_Max;    // 记录鼠标的最大值用来校准鼠标
        float Y_Flt;
        float Y_Filter; // 处理后使用的鼠标值
        float Y_Max;    // 记录鼠标的最大值用来校准鼠标
		float Z_Flt;
        float Z_Filter; // 处理后使用的鼠标值
        float Z_Max;    // 记录鼠标的最大值用来校准鼠标
        uint8_t R_State : 4;
        uint8_t L_State : 4;
        uint8_t R_PressTime;
        uint8_t L_PressTime;
    } Mouse;
	
    struct
    {
        uint8_t W : 4;
        uint8_t S : 4;
        uint8_t A : 4;
        uint8_t D : 4;
        uint8_t Shift : 4;
        uint8_t Ctrl : 4;
        uint8_t Q : 4;
        uint8_t E : 4;
        uint8_t R : 4;
        uint8_t F : 4;
        uint8_t G : 4;
        uint8_t Z : 4;
        uint8_t X : 4;
        uint8_t C : 4;
        uint8_t V : 4;
        uint8_t B : 4;
        uint8_t W_PressTime;
        uint8_t S_PressTime;
        uint8_t A_PressTime;
        uint8_t D_PressTime;
		uint8_t Shift_PreeNumber : 1;
        uint8_t Ctrl_PreeNumber : 1;
        uint8_t Q_PreeNumber : 1;
        uint8_t E_PreeNumber : 1;
        uint8_t R_PreeNumber : 1;
        uint8_t F_PreeNumber : 1;
        uint8_t G_PreeNumber : 1;
        uint8_t Z_PreeNumber : 1;
        uint8_t X_PreeNumber : 1;
        uint8_t C_PreeNumber : 1;
        uint8_t V_PreeNumber : 1;
        uint8_t B_PreeNumber : 1;
    } KeyBoard;
};


union DBUS_UNION_Typdef
{
    struct
    {

     
		uint64_t CH0 : 11;
        uint64_t CH1 : 11;
        uint64_t CH2 : 11;
        uint64_t CH3 : 11;
        uint64_t S1 : 2;
        uint64_t S2 : 2;
		
		int64_t Mouse_X : 16;
        int64_t Mouse_Y : 16;
        int64_t Mouse_Z : 16;
        int64_t Mouse_R : 8;
        int64_t Mouse_L : 8;
		
		uint64_t KeyBoard_W : 1;
        uint64_t KeyBoard_S : 1;
        uint64_t KeyBoard_A : 1;
        uint64_t KeyBoard_D : 1;
        uint64_t KeyBoard_Shift : 1;
        uint64_t KeyBoard_Ctrl : 1;
        uint64_t KeyBoard_Q : 1;
        uint64_t KeyBoard_E : 1;
        uint64_t KeyBoard_R : 1;
        uint64_t KeyBoard_F : 1;
        uint64_t KeyBoard_G : 1;
        uint64_t KeyBoard_Z : 1;
        uint64_t KeyBoard_X : 1;
        uint64_t KeyBoard_C : 1;
        uint64_t KeyBoard_V : 1;
        uint64_t KeyBoard_B : 1;
		
		uint64_t Direction : 11;
		uint64_t : 0;
    } DataNeaten;
    // 接收到的数组
   uint8_t  GetData[19];
};

struct CONTAL_Typedef
{
    struct
    {
        int16_t VX;
        int16_t VY;
        int16_t VW;
        int8_t  VZ;
		int8_t  VR;
    } BOTTOM;
    struct
    {
        float HORIZONTAL;
        float LIFT;
		float SWAY;
		
        float Pitch;
		float A_CUP_PUMP;
		float R_CUP_PUMP;
		float Yaw;
		
		float Camera;
        GPIO_PinState CUP_PUMP;
    } HEAD;
	struct
    {
        float HORIZONTAL;
        float LIFT;
		float SWAY;
		
        float Pitch;
        float Roll;
		float Yaw;
		
        GPIO_PinState CUP_PUMP;
    } User;
	
	struct
	{
		uint16_t pitch;
		uint16_t yaw;
	} TUCHUAN;
	
	uint8_t MOD;
	uint8_t ORE;
};


struct mecanumInit_typdef
{
    float wheel_perimeter;    /* 轮的周长（mm）*/
    float wheeltrack;         /* 轮距（mm）*/
    float wheelbase;          /* 轴距（mm）*/
    float rotate_x_offset;    /* 相对于底盘中心的x轴旋转偏移量(mm) */
    float rotate_y_offset;    /* 相对于底盘中心的y轴旋转偏移量(mm) */
    float deceleration_ratio; /* 电机减速比 */
    int max_vx_speed;         /* 底盘的x轴的最大速度(mm/s) */
    int max_vy_speed;         /* 底盘的y轴的最大速度(mm/s) */
    int max_vw_speed;         /* 底盘的自转的最大速度(degree/s) */
    int max_wheel_ramp;       /* 3508最大转速 */
                              // 每一个轮子的旋转比率//与旋转中心点相关
    float raid_fr;            // 右前
    float raid_fl;            // 左前
    float raid_bl;            // 左后
    float raid_br;            // 右后
    float wheel_rpm_ratio;    // 用来将速度转化成转每分钟
};

extern uint8_t MSE_V_DBUS_STATUS;  
extern uint8_t MSE_V_DBUS_ONLINE_JUDGE_TIME;
extern uint8_t MSE_V_Crotral_ONLINE_JUDGE_TIME;
extern uint8_t DBUS_RX_DATA[19];
extern struct DBUS_Typedef MSE_V_DBUS;
extern struct CONTAL_Typedef MSE_V_CONTAL;
extern struct CONTAL_Typedef MSE_V_CONTAL_Last;
extern struct mecanumInit_typdef mecanumInit_t;
extern uint8_t PID_INIT;
extern uint8_t AIM_INIT;
extern int16_t CHASSIS_VX, CHASSIS_VY, CHASSIS_VW;
extern uint8_t x,y,z,w,m,kit,rots,gpio,hal;
extern uint8_t home_state;
extern uint32_t start_time;  
extern uint32_t first_power,first_left,first_right,send_group;
extern uint32_t current_time;

void MSE_F_DUBS_Resovled(uint8_t* Data);
void MSE_F_DUBSTOCONTAL(void);
float OneFilter(float last , float now , float thresholdValue);
uint8_t MSE_F_KEY_STATUS(uint64_t  KEY , uint8_t PRESS_TIME);

#endif
